<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>es6</title>
</head>
<body>
<script>
    function getNumber(isNumber) {
        if (isNumber) {
            var num = "7";
            // let num = "7";
        } else {
            var notNum = "not number!";
            // let notNum = "not number!";
            console.log(num);
        }
    }
    getNumber(false); // undefined
    console.log("\n");
    // JavaScript 中有一个提升机制
    // function getNumber(isNumber) {
    //     var num;
    //     var notNum;
    //     if (isNumber) {
    //         num = "7";
    //     } else {
    //         notNum = "not number!";
    //         console.log(num);
    //     }
    // }
    // getNumber(false);
    // 因为在开头定义变量时，没有给 num 变量赋任何值，并且 getNumber 传入的是 false，导致 if 语句未执行，num 未被赋值，所以控制台输出 undefined。

    let str1 = `Hello LanQiao!`;
    console.log("输出字符串：" + str1);
    console.log("字符串类型：" + typeof str1);
    console.log("字符串长度：" + str1.length);
    console.log("\n");

    let str2 = `Hello,
ECMAScript 6`;
    console.log(str2);
    console.log("\n");

    let str3 = `LanQiao Courses`;
    let message = `I like ${str3}.`;
    console.log(message);
    console.log("\n");

    let a = 2;
    let b = 1;
    let sum1 = `a+b=${a + b}`;
    console.log(sum1);
    console.log("\n");

    let str4 = "LanQiao Courses";
    console.log("str 字符串中是否存在 Java：" + str4.includes("Java"));
    console.log("str 字符串的首部是否存在字符 Lan：" + str4.startsWith("Lan"));
    console.log("str 字符串的尾部是否存在字符 Course：" + str4.endsWith("Course"));
    console.log("\n");

    let arr1 = new Array(5);
    console.log("数组长度：" + arr1.length);
    console.log(arr1);
    console.log("arr[0]：" + arr1[0]);
    console.log("arr[4]：" + arr1[4]);
    console.log("\n");

    let arr2 = new Array(3, 4);
    console.log("数组长度：" + arr2.length);
    console.log(arr2);
    console.log("arr[0]：" + arr2[0]);
    console.log("arr[1]：" + arr2[1]);
    console.log("\n");

    let arr3 = new Array(4, "7", "8");
    console.log("数组长度：" + arr3.length);
    console.log(arr3);
    console.log("arr[0]：" + arr3[0]);
    console.log("arr[1]：" + arr3[1]);
    console.log("arr[2]：" + arr3[2]);
    console.log("\n");

    let arr4 = Array.of(7);
    console.log(arr4);
    console.log("数组长度：" + arr4.length);
    console.log("arr[0]：" + arr4[0]);
    console.log("\n");

    let arrLike1 = {
        0: "🍎",
        1: "🍐",
        2: "🍊",
        3: "🍇",
        length: 4,
    };
    var arr5 = [].slice.call(arrLike1);
    console.log("arr：" + arr5);
    console.log("\n");

    let arrLike2 = {
        0: "🍎",
        1: "🍐",
        2: "🍊",
        3: "🍇",
        length: 4,
    };
    var arr6 = Array.from(arrLike2);
    console.log("arr：" + arr6);
    console.log("\n");

    let arr7 = [1, 3, 4, 5];
    let result1 = arr7.find(function (value) {
        return value > 2;
    });
    console.log(result1);
    console.log("\n");

    let arr8 = [1, 3, 4, 5];
    arr8.find(function (value, index, arr8) {
        console.log(value > 2);
        console.log(index);
        console.log(arr8);
    });
    console.log("\n");

    let arr9 = ["小猫", "小狗", "兔子"];
    let result2 = arr9.findIndex(function (value, index, arr9) {
        return value == "兔子";
    });
    console.log(result2);
    console.log("\n");

    let arr10 = ["🐱", "🐶", "🐰"];
    let result3 = arr10.fill("🐷");
    console.log(result3);
    console.log("\n");

    let arr11 = ["🐱", "🐶", "🐰", "🐍", "🐦", "🐟"];
    let result4 = arr11.fill("🐷", 2, 5);
    console.log(result4);
    console.log("\n");

    let arr12 = ["🐱", "🐶", "🐰", "🐍", "🐦", "🐟"];
    let result5 = arr12.entries();
    console.log(...result5);
    console.log("\n");

    let arr13 = ["🐱", "🐶", "🐰", "🐍", "🐦", "🐟"];
    let result6 = arr13.keys();
    console.log(result6);
    console.log(...result6);
    console.log("\n");

    let arr14 = ["🐱", "🐶", "🐰", "🐍", "🐦", "🐟"];
    let result7 = arr14.values();
    console.log(result7);
    console.log(...result7);
    console.log("\n");

    const arr15 = ["小红", "小蓝", "小绿"];
    for (let name of arr15) {
        console.log("欢迎" + name + "来到河南农业科技大学！");
    }
    console.log("\n");

    let animals1 = ["兔子🐰", "猫咪🐱"];
    let zoo = [...animals1, "老虎🐯", "乌龟🐢", "鱼🐟"];
    console.log(zoo);
    console.log("\n");

    let animals2 = ["老虎🐯", "乌龟🐢", "鱼🐟"];
    let newAnimals = [...animals2];
    console.log(newAnimals);
    console.log("\n");

    let student1 = { name: "小白", age: 17, email: "1234@qq.com" };
    let NewObj1 = { ...student1 };
    console.log(NewObj1);
    console.log("\n");

    let student2 = { name: "小白", age: 17, email: "1234@qq.com" };
    let NewObj2 = { ...student2, id: 7 };
    console.log(NewObj2);
    console.log("\n");

    let studentName = { name: "小白" };
    let studentAge = { age: 17 };
    let NewObj3 = { ...studentName, ...studentAge };
    console.log(NewObj3);
    console.log("\n");

    function func1(words, name) { // es5
        name = name || "🍎";
        console.log(words, name);
    }
    func1("大家好！我是");
    func1("大家好！我是", "🍐");
    func1("大家好！我是", "");
    console.log("\n");

    function func2(words, name = "🍎") { // es6
        console.log(words, name);
    }
    func2("请给我一个");
    func2("请给我一个", "🍐");
    func2("请给我一个", "");
    console.log("\n");

    function func3(words, name = "🍎") {
        // let words = "我需要一个";
        console.log(words, name);
    }
    func3("请给我一个", "🍐");
    console.log("\n");

    function parameter() {
        return "🖤";
    }
    function func4(words, name = parameter()) {
        console.log(words, name);
    }
    func4("请给我一颗小");
    func4("请给我一颗小", "💗");
    console.log("\n");

    function func5(name, value, mount, { a, b, c, d = "苹果" }) {
        console.log(`${name}用${value}元钱买了${mount}个${d}。`);
        console.log(`${name}用${value}元钱买了${mount}个${c}。`);
    }
    func5("小蓝", 5, 3, {
        a: "西瓜",
        b: "菠萝",
        c: "桃子",
    });
    console.log("\n");

    function func6(a, b, ...rest) {
        console.log(rest);
    }
    // function func6(a,...rest,b){
    //     console.log(rest);
    // }
    func6(1, 2, 3, 4, 5, 6, 7, 8, 10);
    console.log("\n");

    let sum2 = function (a, b) {
        return a + b;
    };
    console.log(sum2(1, 2));
    console.log("\n");

    let sum = (a, b) => a + b;
    console.log(sum(1, 2));
    console.log("\n");

    function Number1() { // es5
        var that = this;
        that.num = 1;
        setInterval(function count() {
            // count 函数指向的是 that 变量
            that.num++;
            console.log(that.num);
        }, 1000);
    }
    // new Number1();

    function Number2() { // es6
        this.num = 1;
        setInterval(() => {
            // this 正确地引用了 Number 对象
            this.num++;
            console.log(this.num);
        }, 1000);
    }
    // new Number2();

    let dogName1 = function () { // es5
        return "😊";
    };
    console.log(dogName1());
    console.log("\n");

    let dogName2 = () => "😊"; // es6
    console.log(dogName2());
    console.log("\n");

    let func7 = (x, y = "🌈") => {
        return `${x}${y}`;
    };
    console.log(func7("请给我一道"));
    console.log(func7("请给我一朵", "🌺"));
    console.log("\n");

    let func8 = a => {
        return a;
    }
    console.log(func8('嗨！我是单参数'));
    console.log("\n");

    let func9 = (a) => a;
    console.log(func9("可以省略我哦～"));
    console.log("\n");

    let student = () => ({ name: "小蓝" });
    console.log(student());
    console.log("\n");

    let func10 = (a, b, ...rest) => {
        console.log(a);
        console.log(b);
        console.log(rest);
    };
    func10(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    console.log("\n");

    function MyClass1(num) {
        this.num = num;
        this.enginesActive = false;
    }
    MyClass1.prototype.startEngines = function () {
        console.log("starting ...");
        this.enginesActive = true;
    };
    const myclass1 = new MyClass1(1);
    myclass1.startEngines();
    console.log("\n");

    class MyClass2 {
        // constructor 方法是类的默认方法
        constructor(num) {
            this.num = num;
            this.enginesActive = false;
        }
        // 相当于 MyClass.prototype.startEngines
        startEngines() {
            console.log("dying...");
            this.enginesActive = true;
        }
    }
    const myclass2 = new MyClass2(1);
    myclass2.startEngines();
    console.log("\n");

    function DogType1(name) {
        this.name = name;
    }
    DogType1.prototype.sayName = function () {
        console.log("大家好！我是一只小" + this.name + "。");
    };
    let dog1 = new DogType1("柯基");
    dog1.sayName();
    console.log(DogType1);
    console.log(DogType1.prototype);
    console.log(dog1);
    console.log(dog1 instanceof DogType1);
    console.log(dog1 instanceof Object);
    console.log("\n");

    // ES6 语法
    let DogType2 = class {
        constructor(name) {
            this.name = name;
        }
        sayName() {
            console.log(`大家好！我是一只小${this.name}。`);
        }
    };
    let dog2 = new DogType2("柯基");
    dog2.sayName();
    console.log(DogType2);
    console.log(DogType2.prototype);
    console.log(dog2);
    console.log(dog2 instanceof DogType2);
    console.log(dog2 instanceof Object);
    console.log("\n");

    let DogName = class MyClass3 {
        constructor(name) {
            this.name = name;
        }
        sayName() {
            console.log(this.name);
        }
    };
    console.log(DogName);
    console.log(DogName.prototype);
    console.log(typeof DogName);
    console.log(typeof MyClass3);
    console.log("\n");

    function Animal1(name, age, speed) { // es5
        this.name = name;
        this.age = age;
        this.speed = speed;
    }
    Animal1.prototype.run = function () {
        console.log(`${this.age}岁的${this.name}大摇大摆地跑了 ${this.speed} 公里。`);
    };
    Animal1.prototype.stop = function () {
        console.log(`${this.name}停止了奔跑。`);
    };
    let dog3 = new Animal1("特朗普", 1 , 100);
    dog3.run();
    dog3.stop();
    console.log("\n");

    class Animal2 { // es6
        constructor(name, age, speed) {
            this.name = name;
            this.age = age;
            this.speed = speed;
        }
        run() {
            console.log(`${this.age}岁的${this.name}酷跑了 ${this.speed} 公里。`);
        }
        stop() {
            console.log(`${this.name}停止了奔跑。`);
        }
    }
    class Dog1 extends Animal2 {
        eat() {
            console.log(`${this.name}吃了一斤屎。`)
        }
    } // Dog 类继承了 Animal 类
    // 实例化 Dog 类
    let dog4 = new Dog1("特朗普", "两", 200);
    console.log(dog4);
    console.log(Dog1.prototype);
    console.log(Animal2.prototype);
    // 调用 Animal 类中的 run() 方法和 stop() 方法
    dog4.run();
    dog4.stop();
    dog4.eat();
    console.log("\n");

    function func11(message) {
        return class {
            say() {
                console.log(message);
            }
        };
    }
    class Person extends func11("欢迎来到河南农业科技大学！") {}
    person = new Person();
    person.say();
    console.log("\n");

    class Animal3 { // es6
        constructor(name, age, speed) {
            this.name = name;
            this.age = age;
            this.speed = speed;
        }
        stop() {
            console.log(`${this.name}停止了奔跑。`);
        }
    }
    class Dog2 extends Animal3 {
        stop() {
            super.stop();
            console.log(`${this.name}累趴了。`);
        }
    }
    let dog5 = new Dog2("特朗普", "两", 200);
    dog5.stop();
    console.log("\n");

    class Animal4 {
        constructor(name, age, speed) {
            this.name = name;
            this.age = age;
            this.speed = speed;
        }
        run() {
            console.log(`${this.age}岁的${this.name}酷跑了 ${this.speed} 公里。`);
        }
    }
    class Dog3 extends Animal3 {
        constructor(name, age, speed, species) {
            super(name,age);
            this.species = species;
        }
        run() {
            console.log(`${this.name}是一只${this.age}岁的奔跑的${this.species}。`);
        }
    }
    let dog6 = new Dog3("拜登", "一", 5, "狗");
    dog6.run();
    console.log("\n");

    function DogType3(name) { // ES5 语法
        this.name = name;
    }
    // 静态方法
    DogType3.create = function (name) {
        return new DogType3(name);
    };
    // 实例方法
    DogType3.prototype.sayName = function () {
        console.log(`大家好！我是一只小${this.name}。`);
    };
    let dog7 = DogType3.create("柯基");
    dog7.sayName();
    console.log("\n");

    // ES6 语法
    class DogType4 {
        constructor(name) {
            this.name = name;
        }
        // 对应 DogType.prototype.sayName
        sayName() {
            console.log(`大家好！我是一只老${this.name}。`);
        }
        // 对应 DogType.create
        static create(name) {
            return new DogType4(name);
        }
    }
    let dog8 = DogType4.create("柯基");
    dog8.sayName();
</script>
</body>
</html>